<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/name.php';

$db = new DB('news');

$title_id = isset($_GET['title_id']) ? h($_GET['title_id']) : 0;
$this_history_id = isset($_GET['this_history_id']) ? intval($_GET['this_history_id']) : 1;//当前版本
$old_history_id = isset($_GET['old_history_id']) ? intval($_GET['old_history_id']) : 0;//调用哪个老版本

//当前文章的最新一版
$news_frist_sql = 'select * from news where title_id="'.$title_id.'" and is_del=1 limit 1';
$news_frist_rs = $db->query($news_frist_sql)->getOne();
//当前栏目名称
$this_category_name = Name::get_catename($db, $news_frist_rs['category_id']);

if($old_history_id > 0){//读取了历史版本
	$news_sql = 'select * from news_history where title_id="'.$title_id.'" and history_id='.$old_history_id.' limit 1';
	$news_rs = $db->query($news_sql)->getOne();
	
	$news_rs['index_top'] = $news_frist_rs['index_top'];
	$news_rs['title_history_id'] = $news_rs['history_id'];
	$news_rs['updated'] = $news_frist_rs['created'];
}else{
	$news_rs = $news_frist_rs;
}

if($news_rs['updated'] < 1367748681)
{
	$news_rs['content'] = h_d($news_rs['content']);
}

$category_id = $news_rs['category_id'];

//查询轨迹
$track_sql = 'select * from news_track where title_id="'.$title_id.'" order by track_id desc';
$track_rs = $db->query($track_sql)->getAll();
//查询姓名
foreach( $track_rs as $key=>$val ){
	$create_username = Name::get_username($db, $val['create_uid']);
	$track_rs[$key]['create_username'] = $create_username;
}

//判断定时发布时间
if($news_rs['is_publish']==1 && $news_rs['publish_time']!=0 && $news_rs['publish_time']<=time()){//历史定时，已过发布时间
	//读取当前发布时间
	$news_rs['is_publish'] = $news_frist_rs['is_publish'];
	$news_rs['publish_time'] = $news_frist_rs['publish_time'];
}
$publish = 0;
if($news_rs['is_publish']==1 && $news_rs['publish_time']>time()){
	$publish = 1;//定时，未发布
}
if($news_rs['is_publish']==2 && $news_rs['publish_time']!=0){
	$publish = 2;//定时，已发布或提早发布
}

//查询大话连体钞期数
$news_rs['periods'] = '';
if( $category_id == 45 ){
	$periods_sql = 'select periods from news_periods where title_id="'.$title_id.'" limit 1';
	$periods_rs = $db->query($periods_sql)->getOne();
	$news_rs['periods'] = $periods_rs['periods'];
}

//版主名人堂
$news_rs['news_mrt_q_a'] = array();
if( $category_id == 46 ){
	$mrt_q_a_sql = 'select id,question,answer,orders from news_mrt_q_a where title_id="'.$title_id.'" and is_del=1 order by orders desc,id asc';
	$mrt_q_a_rs = $db->query($mrt_q_a_sql)->getAll();
	$news_rs['news_mrt_q_a'] = $mrt_q_a_rs;
}

//记录动作日志
Log::logs_articles($category_id, 3, $news_rs['title_id'], $news_rs['title']);

//包含模板
require template('admin/news/news_edit');